package u1project.server;

import u1project.common.Request;
import u1project.common.Response;

import java.io.*;
import java.lang.reflect.Method;
import java.net.Socket;

/**
 * @author oldliu
 * @since 1.0
 */
public class ServerThread implements Runnable {
    private final Socket s;
    private UserManager userManager=new UserManager();
    public ServerThread(Socket accept) {
        this.s = accept;
    }

    public Response login(Request rq) {
        Response resp = new Response();
        String name = (String) rq.get("name");
        String pass = (String) rq.get("pass");
        if (name.equals("admin") && "12345".equals(pass)) {
            resp.setSuccess(true);
            resp.setMsg("成功");
        } else {
            resp.setSuccess(false);
            resp.setMsg("错误的用户名和密码！");
        }
        return resp;
    }
   public Response register(Request rq) {
            Response resp = new Response();
            String name = (String) rq.get("name");
            String pass = (String) rq.get("pass");
            String pass2 = (String) rq.get("repeatpass");

            return userManager.register(name,pass,pass2);
        }
   /* public Response register(Request rq) {
        Response resp = new Response();
        String name = (String) rq.get("name");
        String pass = (String) rq.get("pass");
        String pass2 = (String) rq.get("repeatpass");
        if (!pass.equals(pass2)) {
            resp.setSuccess(false);
            resp.setMsg("两次密码不一致！");
        } else if (pass.length() < 5) {
            resp.setSuccess(false);
            resp.setMsg("密码太短！");
        } else if (name.equals("admin")) {
            resp.setSuccess(false);
            resp.setMsg("用户名已经被使用！");
        } else {
            resp.setSuccess(true);
            resp.setMsg("注册成功");
        }
        return resp;
    }*/
    public Response upload(Request req) {
        //文件名，发文件内容
        String filename =(String)req.get("filename");
        byte[]b=(byte[])req.get("content");
        //保存
        try {
            FileOutputStream fout=new FileOutputStream(new File("D:\\Documents\\桌面\\ffff",filename));
            fout.write(b);
            fout.close();
            Response  r= new Response(true,"文件保存成功");
            r.put("location","在老刘的d盘");
            return r;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Response(false,"文件保存Shibai");
    }
    @Override
    public void run() {
        System.out.println(s + "连接了。。。");
        try {
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            ObjectOutputStream oout = new ObjectOutputStream(out);
            ObjectInputStream oin = new ObjectInputStream(in);
            while (true) {
                Request req = (Request) oin.readObject();
                System.out.println(s + "访问:" + req.getAction());
                Response resp = null;
                //反射代替if else
                Method m=this.getClass().getDeclaredMethod(req.getAction(),Request.class);
                resp =(Response) m.invoke(this, req);
//                if (req.getAction().equals("login")) {
//                    resp = login(req);
//                } else if (req.getAction().equals("register")) {
//                    resp = register(req);
//                } else if (req.getAction().equals("upload")) {
//                    resp = upload(req);
//                } else {
//                    resp=new Response();
//                    resp.setSuccess(false);
//                    resp.setMsg("未实现的操作！");
//                }
                oout.writeObject(resp);
                oout.reset();//!!!!!!!!
                oout.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                s.close();
            } catch (Exception e) {

            }
        }

    }


}
